#!/bin/bash
# network_latency_suite.sh

TARGETS=(
    "43.135.186.249:腾讯云-硅谷"
    "157.230.165.12:DigitalOcean-旧金山"
)

echo "=== 网络延迟综合测试套件 ==="
echo "开始时间: $(date)"
echo ""

for target_info in "${TARGETS[@]}"; do
    IP=$(echo $target_info | cut -d: -f1)
    NAME=$(echo $target_info | cut -d: -f2)
    
    echo "🔍 测试目标: $NAME ($IP)"
    echo "=================================="
    
    # 1. ICMP Ping延迟测试
    echo "1. ICMP Ping延迟测试:"
    ping -c 100 -i 0.1 $IP | tail -2
    echo ""
    
    # 2. TCP握手延迟测试
    echo "2. TCP握手延迟测试:"
    for i in {1..10}; do
        { time nc -zv -w 3 $IP 80 2>&1 | grep succeeded; } 2>&1 | \
        grep real | awk '{printf "测试'$i': %s\n", $2}'
    done
    echo ""
    
    # 3. HTTP请求延迟分解
    echo "3. HTTP请求延迟分解:"
    curl -o /dev/null -s -w \
    "DNS解析: %{time_namelookup}s\nTCP握手: %{time_connect}s\nSSL握手: %{time_appconnect}s\n首字节: %{time_starttransfer}s\n总时间: %{time_total}s\n" \
    http://$IP/
    echo ""
    
    # 4. UDP延迟测试（如支持）
    echo "4. UDP延迟测试:"
    timeout 5 ping -c 5 -i 0.2 $IP 2>/dev/null | tail -2 || echo "UDP测试失败"
    echo ""
    
    echo "---"
done